<?php

class Painel_Form_Produto extends Zend_Form
{

    public function init()
    {
        $front = Zend_Controller_Front::getInstance();
        $controller = $front->getRequest()->getControllerName();
        $module = $front->getRequest()->getModuleName();
        $url = $front->getBaseUrl().'/'.$module.'/'.$controller;
        
        $this->setName('form_edt');
        
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        
        $id = $front->getRequest()->getParam('id', 0);
        
        $el = new Zend_Form_Element_Hidden('PRO_ID',array('disableLoadDefaultDecorators' =>true));
        $el->addDecorators(array(array('ViewHelper'),array('Errors')))
            ->addFilter('Int');
        $this->addElement($el);
        
        $img = glob('public/upload/'.$controller.'/'.$id.'_bar.*');
        if(isset($img[0]))
        foreach($img as $src) {
            $el = new Zend_Form_Element('barra',array( 'helper'=>'formNote', 'value'=>'<img src="'.$front->getBaseUrl().'/'.$src.'" />' ));
            $this->addElement($el);
        }
        
        $el = new Zend_Form_Element_Text('PRO_CODBAR');
        $el->setLabel('Código de Barra');
        $el->setRequired(true);
        $el->setAttrib('class', 'form-control');
        $el->setAttrib('maxlength', '13');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('PRO_NOM');
        $el->setLabel('Nome');
        $el->setRequired(true);
        $el->setAttrib('class', 'form-control required');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('PRO_VLRCST');
        $el->setLabel('Preço de Custo');
        $el->setAttrib('class', 'form-control valor');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('PRO_VLRVND');
        $el->setLabel('Preço de Venda');
        $el->setAttrib('class', 'form-control valor');
        $this->addElement($el);
        
        $arr = array(''=>'Nenhum');
        $query = 'SELECT SQL_CACHE * FROM CATEGORIA_PRODUTO WHERE CTP_STA = 1 ORDER BY CTP_NOM ASC';
        $result = $db->fetchAll($query);
        foreach($result as $r) $arr[$r['CTP_ID']] = $r['CTP_NOM'];
        $el = new Zend_Form_Element_Select('CTP_ID',array('onchange' => ''));
        $el->setLabel('Categoria')
        ->addMultiOptions($arr);
        $el->setRegisterInArrayValidator(false);
        $el->setAttrib('class', 'form-control required');
        $this->addElement($el);
        
        $arr = array(''=>'Nenhum');
        $query = 'SELECT SQL_CACHE * FROM UNIDADE_MEDIDA WHERE UNM_STA = 1 ORDER BY UNM_NOM ASC';
        $result = $db->fetchAll($query);
        foreach($result as $r) $arr[$r['UNM_ID']] = $r['UNM_SGL'].' - '.$r['UNM_NOM'];
        $el = new Zend_Form_Element_Select('UNM_ID',array('onchange' => ''));
        $el->setLabel('Unidade')
        ->addMultiOptions($arr);
        $el->setRegisterInArrayValidator(false);
        $el->setAttrib('class', 'form-control required');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('PRO_QTD');
        $el->setLabel('Quantidade');
        $el->setRequired(true);
        $el->setAttrib('class', 'form-control valor');
        $this->addElement($el);
        
        $arr = array(''=>'Nenhum');
        $query = 'SELECT SQL_CACHE * FROM FORNECEDOR WHERE FOR_STA = 1 ORDER BY FOR_NOM ASC';
        $result = $db->fetchAll($query);
        foreach($result as $r) $arr[$r['FOR_ID']] = $r['FOR_NOM'];
        $el = new Zend_Form_Element_Select('FOR_ID',array('onchange' => ''));
        $el->setLabel('Fornecedor')
        ->addMultiOptions($arr);
        $el->setRegisterInArrayValidator(false);
        $el->setAttrib('class', 'form-control required');
        $this->addElement($el);
        
        $img = glob('public/upload/'.$controller.'/'.$front->getRequest()->getParam('id').'.*');
        if(!isset($img[0])) $img = glob('public/media/images/sem_imagem.*');
        foreach($img as $src) {
            $el = new Zend_Form_Element('img',array( 'helper'=>'formNote', 'value'=>'<img src="'.$front->getBaseUrl().'/'.$src.'" />' ));
            $this->addElement($el);
        }
        $el = new Zend_Form_Element_File('PRO_FILE');
        $el->setLabel('Imagem')
	->addValidator('Extension', false, array('jpg', 'png', 'gif'));
        $el->setAttrib('class', 'form-control');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Checkbox('PRO_STA',array('checked'=>'checked'));
        $el->setLabel('Ativo');
        $this->addElement($el);
        
        $el = new Zend_Form_Element('br',array( 'helper'=>'formNote', 'value'=>'&nbsp;' ));
        $this->addElement($el);
        
        $btn = new Zend_Form_Element_Button('Salvar',array('disableLoadDefaultDecorators' =>true));
        $btn->setDecorators(array(array('Description', array('escape' => false, 'tag' => false))));
        $btn->setDescription('<button type="submit" class="btn btn-lg btn-success"><span class="glyphicon glyphicon-floppy-saved" aria-hidden="true"></span> Salvar</button>');
        $this->addElement($btn);
        
        $btn = new Zend_Form_Element_Button('Cancelar',array('disableLoadDefaultDecorators' =>true));
        $btn->setDecorators(array(array('Description', array('escape' => false, 'tag' => false))));
        $btn->setDescription('<a href="'.$url.'" class="btn btn-lg btn-danger"><span class="glyphicon glyphicon-floppy-remove" aria-hidden="true"></span> Cancelar</a>');
        $this->addElement($btn);
     
    }


}

